using System;
using UnityEngine;

public class KICOITAdvancedLogic : KICOITConsultant {
	public override void OnAwake () {
		base.OnAwake ();
		displayName = "KICOIT Advanced Stats";
	}

	public override void AnalizePart (Part p, ShipData s) {
		base.AnalizePart (p, s);
	}

	public override void DisplayUI (ShipData s) {
		GUIStyle sty = new GUIStyle (GUI.skin.label);
		sty.alignment = TextAnchor.UpperRight;
		
		GUILayout.BeginVertical ();
		GUILayout.BeginHorizontal ();
		//GUILayout.FlexibleSpace ();
		GUILayout.Label ("                Thrust to Weight Ratios");
		GUILayout.FlexibleSpace ();
		GUILayout.EndHorizontal ();
		GUILayout.BeginHorizontal ();
		// stages only if they have thrust
		
		GUILayout.BeginVertical ();
		GUILayout.Label ("Stage");
		for (int i = 0; i <s.Count(); i++) {
			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0)
				GUILayout.Label ("S " + i);
		}
		GUILayout.EndVertical ();
		//total weight
//		GUILayout.BeginVertical ();
//		GUILayout.Label ("Mass");
//		for (int i = 0; i <s.Count(); i++) {
//			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0)
//				GUILayout.Label (s.getStage (i).totalWeight.ToString ("0.##"), sty);
//		}
//		GUILayout.EndVertical ();
		// kerbin twr
		GUILayout.BeginVertical ();
		GUILayout.Label ("Kerbin");
		for (int i = 0; i <s.Count(); i++) {
			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0) {
				if (s.getStage (i).thrust != 0 && s.getStage (i).airthrust != 0) {
					sty.normal.textColor = Color.yellow;//mixed
				} else if (s.getStage (i).airthrust != 0) {
					sty.normal.textColor = Color.green;//air
				} else {
					sty.normal.textColor = Color.white;//rockets
				}
				float ktw = (s.getStage (i).thrust + s.getStage (i).airthrust) / s.getStage (i).totalWeight;
								
				GUILayout.Label (
							(ktw / ShipData.KerbinGravity).ToString ("0.##"),
							sty
				);
			}	
		}
		GUILayout.EndVertical ();
		sty.normal.textColor = Color.white;
		//mun twr
		GUILayout.BeginVertical ();
		GUILayout.Label ("Mun");
		for (int i = 0; i <s.Count(); i++) {
			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0) {
				float tw = s.getStage (i).thrust / s.getStage (i).totalWeight;		
				GUILayout.Label ((tw / ShipData.MunGravity).ToString ("0.##"), sty);
			}
		}
		GUILayout.EndVertical ();
		//minmus twr
		GUILayout.BeginVertical ();
		GUILayout.Label ("Minmus");
		for (int i = 0; i <s.Count(); i++) {
			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0) {
				float tw = s.getStage (i).thrust / s.getStage (i).totalWeight;		
				GUILayout.Label ((tw / ShipData.MinMusGravity).ToString ("0.##"), sty);
			}
				
		}
		GUILayout.EndVertical ();
		//Isp
		GUILayout.BeginVertical ();
		GUILayout.Label ("Isp");
		for (int i = 0; i <s.Count(); i++) {
			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0)
				GUILayout.Label (s.getStage (i).Isp.ToString ("0.#"));
		}
		GUILayout.EndVertical ();
		//delta-v
		GUILayout.BeginVertical ();
		GUILayout.Label ("Delta-V");
		for (int i = 0; i <s.Count(); i++) {
			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0)
				GUILayout.Label (s.getStage (i).deltaV.ToString ("0.#"));
		}
		GUILayout.EndVertical ();
		GUILayout.EndHorizontal ();
		GUILayout.EndVertical ();
	}

	public override void AnalizeShip (ShipData s) {
		float fuelmass = 0;
		float fuel = 0;

		for (int i = 0; i < s.Count(); i++) {
			//get mass of fuel
			fuelmass += s.getStage (i).fuelMass;
			fuel += s.getStage (i).fuel;
			if (s.getStage (i).thrust > 0 || s.getStage (i).airthrust > 0) {
				//fueldensity = divide by total fuel units
				Debug.Log("fuelMass: "+fuelmass);
				Debug.Log("fuel: "+fuel);
				float fueldensity = fuelmass / fuel;
				Debug.Log("fueldensity: "+fueldensity);
				//get total fuel consuption
				float totalConsuption = 0;
				
				if (s.getStage (i).partlist.ContainsKey ("LiquidEngine")) {
					Debug.Log("total lfe consumption");
					foreach (Part p in s.getStage(i).partlist["LiquidEngine"]) {
						totalConsuption += (p as LiquidEngine).fuelConsumption;
					}
				}
				if (s.getStage (i).partlist.ContainsKey ("AtmosphericEngine")) {
					Debug.Log("total atm consumption");
					foreach (Part p in s.getStage(i).partlist["AtmosphericEngine"]) {
						totalConsuption += (p as AtmosphericEngine).fuelConsumption;
					}
				}
				
				float masspersecond = totalConsuption * fueldensity;
				
				//account for solidrocket parts probably very inaccurate way of doing this.
				if (s.getStage(i).partlist.ContainsKey("SolidRocket")) {
					foreach (Part p in s.getStage(i).partlist["SolidRocket"]) {
						float solidfuelmass = (p as SolidRocket).mass - (p as SolidRocket).dryMass;
						fuelmass += solidfuelmass;
						masspersecond += (p as SolidRocket).fuelConsumption *(solidfuelmass / (p as SolidRocket).internalFuel);
						//thrust is included in the stage total already
					}
				}
				Debug.Log("totalconsumption: "+ totalConsuption);
				//mass per second = total fuel consumption / fueldensity
				
				Debug.Log("masspersecond: "+masspersecond);
				
				
				float exhaustVelocity = (s.getStage (i).thrust + s.getStage(i).airthrust) / masspersecond;
				Debug.Log("Isp: " + exhaustVelocity);
				//calculate Dv
				//deltaV = ve * ln(fullmass/emptymass);
				float deltaV = exhaustVelocity * Mathf.Log (s.getStage (i).totalWeight / (s.getStage (i).totalWeight - fuelmass));
				Debug.Log("deltaV: "+ deltaV);
				s.getStage (i).Isp = exhaustVelocity / ShipData.KerbinGravity;
				s.getStage (i).deltaV = deltaV;
				
				//reset fuelmass counters
				fuelmass = 0;
				fuel = 0;
			}
		}
	}		
}